import axios from 'axios';
// import joinUrl from 'url-join';

const api = axios.create();

api.interceptors.request.use(
  (config) => {
    const prefix = window.blocklet ? window.blocklet.prefix : '/';
    config.baseURL = prefix || '';
    config.timeout = 200000;

    return config;
  },
  (error) => Promise.reject(error)
);

export default api;

export function handlerParams(data) {
  if (data === null || data === undefined || data === '') {
    data = {}
  }
  let keys = Object.keys(data).sort();
  let str = ''
  for (let i in keys) {
    if (keys[i] === "signature") {
      continue
    }
    if (data[keys[i]] === null || data[keys[i]] === '' || data[keys[i]] === undefined) {
      continue
    }
    str = str + keys[i] + '=' + data[keys[i]] + '&'
  }
  return '?' + str
}
